package com.edu.controller;

import com.edu.bean.Book;
import com.edu.service.BookService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

@Controller
public class BookController {

    @Resource
    BookService bookService;

    @ResponseBody
    @GetMapping("/getBooks")
    public Object getBooks(Book book,
                           @RequestParam(required = false,defaultValue = "1")int pageSize,
                           @RequestParam(required = false,defaultValue = "5")int pageNum,
                           Date startDate,
                           Date endDate
                            ){

        PageHelper.startPage(pageNum,pageSize);
        Page<Book> page = (Page<Book>)bookService.getBooks(book, startDate, endDate);
        return page.toPageInfo();
    }

    @PostMapping("/addBook")
    public String addBook(Book book, MultipartFile mf, Model model) {
        if (mf == null || mf.isEmpty() || !mf.getContentType().contains("image")) {
            model.addAttribute("msg", "必须选择封面，格式为图片");
            return "forward:/addBook.jsp";
        }
        try {
            String fileName = mf.getOriginalFilename();
            String newName = UUID.randomUUID() + fileName;
            String visitPath = "/demo002/bookCover/" + newName;
            String savePath = "c:/coverImgs/" + newName;
            mf.transferTo(new File(savePath));
            book.setBookCover(visitPath);
            bookService.addbook(book);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "redirect:/book.jsp";
    }


    @RequestMapping("/deleteBook")
    @ResponseBody
    public String deleteBook(int bookId) {
        Book book = bookService.getBookById(bookId);

        int result = bookService.deleteBook(bookId);
        if (result == 1) {
            String visitPath = book.getBookCover();
            String realPath = visitPath.replaceAll("/demo002/", "c:/");
            if (!realPath.equals("c:/coverImgs/default.jpg")) {
                new File(realPath).delete();
            }
        }
        return result == 1 ? "success" : "fail";
    }


    @RequestMapping("/updateBook")
    public String updateBook(Book book, MultipartFile mf) {
        bookService.updateBook(book);
        if (mf != null && !mf.isEmpty() && mf.getContentType().contains("image")) {
            // 查出修改后的书的信息，目的是获取到原来的封面路径
            book = bookService.getBookById(book.getBookId());
            // 删除掉原来的封面的图片
            String visitPath = book.getBookCover();
            String realPath = visitPath.replaceAll("/demo002/", "c:/");
            if (!realPath.equals("c:/coverImgs/default.jpg")) {
                new File(realPath).delete();
            }
            // 把用户提交的新封面上传到服务器
            String fileName = mf.getOriginalFilename();
            String newName = UUID.randomUUID() + fileName;
            String visit = "/demo002/cover/" + newName;
            String savePath = "c:/coverImgs/" + newName;
            try {
                mf.transferTo(new File(savePath));
            } catch (IOException e) {
                e.printStackTrace();
            }
            // 数据库修改书的新封面的访问路径
            bookService.updateBookCover(book.getBookId(), visit);
        }
        return "redirect:/book.jsp";
    }

}
